package com.huitone.gddw.service.impl.analysis;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.analysis.ISmSubmitFailDao;
import com.huitone.gddw.dao.entity.SendSm;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.ISmSubmitFailService;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.AllCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.DayCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.GateAllCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.GateCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.GateDayCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.GateHh24Count;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.Hh24Count;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDTO.SvrCount;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.AllDetail;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.GateAllDetail;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.GateDetail;
import com.huitone.gddw.service.dto.analysis.SmSubmitFailDetailDTO.SvrDetail;
import com.huitone.gddw.service.dto.business.SendSmCountDTO;

@Service
public class SmSubmitFailService implements ISmSubmitFailService{

	@Autowired
	private ISmSubmitFailDao smSubmitFailDao;
	
	@Override
	public QueryPage<SmSubmitFailDTO> selectPage(QueryPage<SendSm> page) {
		
		return null;
	}

	
	@Override
	//统计查询
	public HashMap queryCount(Map<String, Object> params) {
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		params.put("includeToday","no");
		if((endDate!=null&&!endDate.equals(""))){
			DateFormat format = new SimpleDateFormat("YYYY-MM-dd");
			
			if(endDate.substring(0,10).equals(format.format(new Date()))){
				params.put("includeToday","yes");
			}
			
		}
		
		String svrNodes = (String) params.get("svrNodes");
		String gateLinkIds = (String) params.get("gateLinkIds");
		if (svrNodes!=null&&svrNodes!="") {
			params.put("svrNodes", svrNodes.split(","));
		}
		if (gateLinkIds!=null&&gateLinkIds!="") {
			params.put("gateLinkIds", gateLinkIds.split(","));
		}
		
		
			if(params.get("type").equals("svr")){
				
				//业务端
				
				
				if ((startDate!=null&&endDate!=null&&startDate.equals(endDate))||(startDate==""&&endDate=="")) {
					List<SvrCount> svrCount = smSubmitFailDao.querySvrCount(params);
					List<DayCount> hh24Count = smSubmitFailDao.queryHh24Count(params);
					List<AllCount> allCount = smSubmitFailDao.queryAllCount(params);
			
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("svrCount", svrCount);
					hashMap.put("dayCount", hh24Count);
					hashMap.put("allCount", allCount);	
					return hashMap;
					
				}
				
					List<SvrCount> svrCount = smSubmitFailDao.querySvrCount(params);
					List<DayCount> dayCount = smSubmitFailDao.queryDayCount(params);
					
					List<AllCount> 	allCount = smSubmitFailDao.queryAllCount(params);
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("svrCount", svrCount);
					hashMap.put("dayCount", dayCount);
					hashMap.put("allCount", allCount);	
					return hashMap;
		}
		
		
			if(params.get("type").equals("gate")){
				//通道
				
if ((startDate!=null&&endDate!=null&&startDate.equals(endDate))||(startDate==""&&endDate=="")) {
					
	List<GateCount> gateCount = smSubmitFailDao.queryGateCount(params);
	List<GateDayCount> gateHh24Count = smSubmitFailDao.queryGateHh24Count(params);
	List<GateAllCount> gateAllCount = smSubmitFailDao.queryGateAllCount(params);
	
	
	
	HashMap<String,Object> hashMap = new HashMap<>();
	hashMap.put("gateCount", gateCount);
	hashMap.put("gateDayCount", gateHh24Count);
	hashMap.put("gateAllCount", gateAllCount);	
			return hashMap;
					
				}
				

				
				
				List<GateCount> gateCount = smSubmitFailDao.queryGateCount(params);
				List<GateDayCount> gateDayCount = smSubmitFailDao.queryGateDayCount(params);
				
				List<GateAllCount> gateAllCount = smSubmitFailDao.queryGateAllCount(params);
				HashMap<String,Object> hashMap = new HashMap<>();
				hashMap.put("gateCount", gateCount);
				hashMap.put("gateDayCount", gateDayCount);
				hashMap.put("gateAllCount", gateAllCount);	
				return hashMap;
			}
		return new HashMap();
		
	}

	
	//分页统计查询
	@Override
	//统计查询
	public HashMap queryPageCount(QueryPage qp) {
		Map params = qp.getParams();
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		params.put("includeToday","no");
		if((endDate!=null&&!endDate.equals(""))){
			DateFormat format = new SimpleDateFormat("YYYY-MM-dd");
			
			if(endDate.substring(0,10).equals(format.format(new Date()))){
				params.put("includeToday","yes");
			}
			
		}
		
		String svrNodes = (String) params.get("svrNodes");
		String gateLinkIds = (String) params.get("gateLinkIds");
		if (svrNodes!=null&&svrNodes!="") {
			params.put("svrNodes", svrNodes.split(","));
		}
		if (gateLinkIds!=null&&gateLinkIds!="") {
			params.put("gateLinkIds", gateLinkIds.split(","));
		}
		
		
			if(params.get("type").equals("svr")){
				
				//业务端
				
				
				if ((startDate!=null&&endDate!=null&&startDate.equals(endDate))||(startDate==""&&endDate=="")) {
					Object allCount=null;
					List<SvrCount> svrCount = smSubmitFailDao.querySvrCount(params);
					List<DayCount> hh24Count = smSubmitFailDao.queryHh24Count(params);
						params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
						params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
						
						List<AllCount> result = smSubmitFailDao.queryPageAllCount(params);
						 allCount = new QueryPage<>();
						 ((QueryPage<AllCount>) allCount).setResult(result);
						 ((QueryPage<AllCount>) allCount).setPageNo(qp.getPageNo());
						 ((QueryPage<AllCount>) allCount).setPageSize(qp.getPageSize());
						 ((QueryPage<AllCount>) allCount).setTotalCount(smSubmitFailDao.queryAllCount(params).size());					
					
					

					
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("svrCount", svrCount);
					hashMap.put("dayCount", hh24Count);
					hashMap.put("allCount", allCount);	
					return hashMap;
					
				}
				
					List<SvrCount> svrCount = smSubmitFailDao.querySvrCount(params);
					List<DayCount> dayCount = smSubmitFailDao.queryDayCount(params);
					Object allCount=null;
							params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
							params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
							
							List<AllCount> result = smSubmitFailDao.queryPageAllCount(params);
							 allCount = new QueryPage<>();
							 ((QueryPage<AllCount>) allCount).setResult(result);
							 ((QueryPage<AllCount>) allCount).setPageNo(qp.getPageNo());
							 ((QueryPage<AllCount>) allCount).setPageSize(qp.getPageSize());
							 ((QueryPage<AllCount>) allCount).setTotalCount(smSubmitFailDao.queryAllCount(params).size());					
		
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("svrCount", svrCount);
					hashMap.put("dayCount", dayCount);
					hashMap.put("allCount", allCount);	
					return hashMap;
		}
		
		
			if(params.get("type").equals("gate")){
				//通道
				
if ((startDate!=null&&endDate!=null&&startDate.equals(endDate))||(startDate==""&&endDate=="")) {
					
	List<GateCount> gateCount = smSubmitFailDao.queryGateCount(params);
	List<GateDayCount> gateHh24Count = smSubmitFailDao.queryGateHh24Count(params);
	Object gateAllCount=null;
	
	if(params.get("excel")!=null&&params.get("excel").equals("excel")){
		gateAllCount = smSubmitFailDao.queryGateAllCount(params);
		}else{
			params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
			params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
			
			List<GateAllCount> result = smSubmitFailDao.queryPageGateAllCount(params);
			gateAllCount = new QueryPage<>();
			 ((QueryPage<GateAllCount>) gateAllCount).setResult(result);
			 ((QueryPage<GateAllCount>) gateAllCount).setPageNo(qp.getPageNo());
			 ((QueryPage<GateAllCount>) gateAllCount).setPageSize(qp.getPageSize());
			 ((QueryPage<GateAllCount>) gateAllCount).setTotalCount(smSubmitFailDao.queryGateAllCount(params).size());					
		
		}
	
	
	
	HashMap<String,Object> hashMap = new HashMap<>();
	hashMap.put("gateCount", gateCount);
	hashMap.put("gateDayCount", gateHh24Count);
	hashMap.put("gateAllCount", gateAllCount);	
			return hashMap;
					
				}
				

				
				
				List<GateCount> gateCount = smSubmitFailDao.queryGateCount(params);
				List<GateDayCount> gateDayCount = smSubmitFailDao.queryGateDayCount(params);
				
				Object gateAllCount=null;
				if(params.get("excel")!=null&&params.get("excel").equals("excel")){
					gateAllCount = smSubmitFailDao.queryGateAllCount(params);
					}else{
						params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
						params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
						
						List<GateAllCount> result = smSubmitFailDao.queryPageGateAllCount(params);
						gateAllCount = new QueryPage<>();
						 ((QueryPage<GateAllCount>) gateAllCount).setResult(result);
						 ((QueryPage<GateAllCount>) gateAllCount).setPageNo(qp.getPageNo());
						 ((QueryPage<GateAllCount>) gateAllCount).setPageSize(qp.getPageSize());
						 ((QueryPage<GateAllCount>) gateAllCount).setTotalCount(smSubmitFailDao.queryGateAllCount(params).size());					
					
					}
				
				
				HashMap<String,Object> hashMap = new HashMap<>();
				hashMap.put("gateCount", gateCount);
				hashMap.put("gateDayCount", gateDayCount);
				hashMap.put("gateAllCount", gateAllCount);	
				return hashMap;
			}
		return new HashMap();
		
	}

	

	@Override
	//明细查询
	public HashMap queryCountDetail(Map<String, Object> params) {
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		params.put("includeToday","no");
		if((endDate!=null&&!endDate.equals(""))){
			DateFormat format = new SimpleDateFormat("YYYY-MM-dd");
			
			if(endDate.substring(0,10).equals(format.format(new Date()))){
				params.put("includeToday","yes");
			}
			
		}
		String svrNodes = (String) params.get("svrNodes");
		String gateLinkIds = (String) params.get("gateLinkIds");
		String errorTypes = (String) params.get("errorTypes");
		if (svrNodes!=null&&svrNodes!="") {
			params.put("svrNodes", svrNodes.split(","));
		}
		if (gateLinkIds!=null&&gateLinkIds!="") {
			params.put("gateLinkIds", gateLinkIds.split(","));
		}
		if (errorTypes!=null&&errorTypes!="") {
			params.put("errorTypes", errorTypes.split(","));
		}
		
		
		if(params.get("type").equals("svr")){
			
			//业务端
				List<SvrDetail> svrDetail = smSubmitFailDao.querySvrDetail(params);
				List<AllDetail> allDetail = smSubmitFailDao.queryAllDetail(params);
					
				HashMap<String,Object> hashMap = new HashMap<>();
				hashMap.put("svrDetail", svrDetail);
				hashMap.put("allDetail", allDetail);
				return hashMap;
				}
				//通道
		if(params.get("type").equals("gate")){
			//通道
					List<GateDetail> gateDetail = smSubmitFailDao.queryGateDetail(params);
					List<GateAllDetail> gateAllDetail = smSubmitFailDao.queryGateAllDetail(params);
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("gateDetail", gateDetail);
					hashMap.put("gateAllDetail", gateAllDetail);
					return hashMap;
					}
				return new HashMap();
	}

	@Override
	//分页明细查询
	public HashMap queryPageCountDetail(QueryPage qp) {
		Map<String,Object> params = qp.getParams();
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		params.put("includeToday","no");
		if((endDate!=null&&!endDate.equals(""))){
			DateFormat format = new SimpleDateFormat("YYYY-MM-dd");
			
			if(endDate.substring(0,10).equals(format.format(new Date()))){
				params.put("includeToday","yes");
			}
			
		}
		String svrNodes = (String) params.get("svrNodes");
		String gateLinkIds = (String) params.get("gateLinkIds");
		String errorTypes = (String) params.get("errorTypes");
		if (svrNodes!=null&&svrNodes!="") {
			params.put("svrNodes", svrNodes.split(","));
		}
		if (gateLinkIds!=null&&gateLinkIds!="") {
			params.put("gateLinkIds", gateLinkIds.split(","));
		}
		if (errorTypes!=null&&errorTypes!="") {
			params.put("errorTypes", errorTypes.split(","));
		}
		
		
		if(params.get("type").equals("svr")){
			
			//业务端
				List<SvrDetail> svrDetail = smSubmitFailDao.querySvrDetail(params);

				Object allDetail=null;
				
						params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
						params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
						
						List<AllDetail> result = smSubmitFailDao.queryPageAllDetail(params);
						allDetail = new QueryPage<>();
						 ((QueryPage<AllDetail>) allDetail).setResult(result);
						 ((QueryPage<AllDetail>) allDetail).setPageNo(qp.getPageNo());
						 ((QueryPage<AllDetail>) allDetail).setPageSize(qp.getPageSize());
						 ((QueryPage<AllDetail>) allDetail).setTotalCount(smSubmitFailDao.queryAllDetail(params).size());					
				
				HashMap<String,Object> hashMap = new HashMap<>();
				hashMap.put("svrDetail", svrDetail);
				hashMap.put("allDetail", allDetail);
				return hashMap;
				}
				//通道
		if(params.get("type").equals("gate")){
			//通道
					List<GateDetail> gateDetail = smSubmitFailDao.queryGateDetail(params);
					Object gateAllDetail=null;
					
							params.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
							params.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
							
							List<GateAllDetail> result = smSubmitFailDao.queryPageGateAllDetail(params);
							gateAllDetail = new QueryPage<>();
							 ((QueryPage<GateAllDetail>) gateAllDetail).setResult(result);
							 ((QueryPage<GateAllDetail>) gateAllDetail).setPageNo(qp.getPageNo());
							 ((QueryPage<GateAllDetail>) gateAllDetail).setPageSize(qp.getPageSize());
							 ((QueryPage<GateAllDetail>) gateAllDetail).setTotalCount(smSubmitFailDao.queryGateAllDetail(params).size());					
					
					
					
					HashMap<String,Object> hashMap = new HashMap<>();
					hashMap.put("gateDetail", gateDetail);
					hashMap.put("gateAllDetail", gateAllDetail);
					return hashMap;
					}
				return new HashMap();
	}


	
	
	
	
	
	
	
	
	

}
